package com.example.hyw.szysteam.arithmetic;

import java.util.ArrayList;
import java.util.Random;

/**
 * Created by HYW on 2018/6/11.
 */

public class AC {
    public ArrayList<Object> createAc(int operatorNo,int range){
        ArrayList<Object> ac=new ArrayList<>();
        MyNumber number=new MyNumber();
        Operator operator=new Operator();
        Random random=new Random();
        int bracket=0;
        int bracketNo=0;
        for(int i=1;i<=operatorNo+1;i++){
            if(bracketNo!=0 && (bracket=random.nextInt(3))!=0 && ac.get(ac.size()-1)!="("){
                if(ac.size()!=0 && ac.get(ac.size()-1).toString()=="÷"){
                    do {
                        number=number.createNumber(10);
                    }while (number.toString().equalsIgnoreCase("0"));
                }else {
                    number.createNumber(range);
                }
                ac.add(number);
                ac.add(")");
                ac.add(operator.createOperator());
                bracketNo--;
            }else if(i!=operatorNo+1){
                bracket=random.nextInt(3);
                if (bracket==0){
                    if(ac.size()!=0 && ac.get(ac.size()-1).toString()=="÷"){
                        do {
                            number=number.createNumber(10);
                        }while (number.toString().equalsIgnoreCase("0"));
                    }else {
                        number=number.createNumber(range);
                    }
                    ac.add(number);
                    ac.add(operator.createOperator());
                }else if(bracket==1){
                    ac.add(0,"(");
                    if(ac.size()!=0 && ac.get(ac.size()-1).toString()=="÷"){
                        do {
                            number=number.createNumber(10);
                        }while (number.toString().equalsIgnoreCase("0"));
                    }else {
                        number.createNumber(range);
                    }
                    ac.add(number);
                    ac.add(operator.createOperator());
                    bracketNo++;
                }else {
                    if(ac.size()!=0 && ac.get(ac.size()-1).toString()=="÷"){
                        do {
                            number=number.createNumber(10);
                        }while (number.toString().equalsIgnoreCase("0"));
                    }else {
                        number=number.createNumber(range);
                    }
                    ac.add(number);
                    ac.add(operator.createOperator());
                }
            }
        }
        if(bracketNo!=0){
            ac.remove(ac.size()-1);
            for(int i=0;i<bracketNo;bracketNo--){
                ac.add(")");
            }
        }else {
            ac.remove(ac.size()-1);
        }
        while (ac.get(0)=="("){
            bracket=0;
            int bracketLeng=0;
            String o="";
            for(int j=0;j<ac.size();j++){
                o=ac.get(j).toString();
                if(o=="("){
                    bracket++;
                    bracketLeng--;
                }
                if (o==")"){
                    bracket--;
                    bracketLeng--;
                }
                if(bracket==0 && bracketLeng!=(2*operatorNo)){
                    return ac;
                }
                if(bracket>=1){
                    bracketLeng++;
                }
            }
            if (bracketLeng==(2*operatorNo)){
                ac.remove(0);
                ac.remove(ac.size()-1);
            }
        }
        return ac;
    }

    public static String toString(ArrayList<Object> list){
        String s1="";
        for(int i=0;i<list.size();i++){
            s1+=list.get(i).toString();
        }
        return s1;
    }
}
